﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace UniversalApproach.Helpers
{
    public static class ServerHelper
    {
        /// <summary>
        /// 获取本地IP地址
        /// </summary>
        /// <param name="ipType">IP类型：<see cref=" System.Net.Sockets.AddressFamily.InterNetwork"/>IPv4 <see cref=" System.Net.Sockets.AddressFamily.InterNetworkV6"/>IPv6 </param>
        /// <returns></returns>
        public static string GetLocalhostIp(System.Net.Sockets.AddressFamily ipType = System.Net.Sockets.AddressFamily.InterNetwork)
        {
            var ipEntry = Dns.GetHostEntry(Dns.GetHostName());
            return ipEntry.AddressList.FirstOrDefault(s => s.AddressFamily == ipType)?.ToString();
        }

        /// <summary>
        /// 获取电脑 MAC地址
        /// </summary>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static List<string> GetActiveMacAddress(string separator = "-")
        {
            //本地计算机网络连接信息
            IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
            //获取本机电脑名
            var HostName = computerProperties.HostName;
            //获取域名
            var DomainName = computerProperties.DomainName;
            //获取本机所有网络连接
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
 
            var macAddress = new List<string>();
            if (nics == null || nics.Length < 1)
            {
                //Debug.WriteLine("  No network interfaces found.");
                return macAddress;
            }
 
            //Debug.WriteLine("  Number of interfaces .................... : {0}", nics.Length);
            foreach (NetworkInterface adapter in nics.Where(c =>
                         c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up))
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();
 
                var unicastAddresses = properties.UnicastAddresses;
                if (unicastAddresses.Any(temp => temp.Address.AddressFamily == AddressFamily.InterNetwork))
                {
                    var address = adapter.GetPhysicalAddress();
 
                    if (string.IsNullOrEmpty(separator))
                    {
                        macAddress.Add(address.ToString());
                    }
                    else
                    {
                        var MACIp = "";
                        byte[] bytes = address.GetAddressBytes();
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            MACIp += bytes[i].ToString("X2");
 
                            if (i != bytes.Length - 1)
                            {
                                MACIp += separator;
                            }
                        }
 
                        macAddress.Add(MACIp);
                    }
                }
            }
            return macAddress;
        }
    }
}